package com.wuliu.servlet;

import com.wuliu.dao.SecondImageDAO;
import com.wuliu.dao.impl.SecondImageDAOImpl;
import com.wuliu.model.SecondImage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/second_image/update")
public class AdminSecondImageUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id= Integer.valueOf(req.getParameter("id"));
        SecondImageDAO secondImageDAO =new SecondImageDAOImpl();
        SecondImage secondImage =secondImageDAO.findById(id);
        req.setAttribute("secondImage",secondImage);
        req.getRequestDispatcher("/WEB-INF/view/admin/second_image_update.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id= Integer.valueOf(req.getParameter("id"));
        String img=req.getParameter("img");
        SecondImage secondImage =new SecondImage(id,img);
        SecondImageDAO secondImageDAO =new SecondImageDAOImpl();
        secondImageDAO.update(secondImage);
        resp.sendRedirect("/admin/second_image");

    }
}
